﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedGlove.Lib;

namespace RGPWEB
{
    public partial class Register : System.Web.UI.Page
    {
        //是否给用户选择身份分组的状态
        public string isrole = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            btn_createuser.Attributes.Add("onclick", "return CheckReg()");
            if (!IsPostBack)
            {
                Response.Cookies.Add(new HttpCookie("VerifyCode", ""));
                if (!RGP_Value.IsVerifyUser)
                {
                    isrole = "true";
                    bind();
                }
                else
                {
                    isrole = "false";
                }
            }
        }

        protected void bind()
        {
            RGP.BLL.RGP_Roles rrBll = new RGP.BLL.RGP_Roles();

            RoleList.DataSource = rrBll.GetRoleList("", "");
            RoleList.DataTextField = "RoleName";
            RoleList.DataValueField = "RoleID";
            RoleList.DataBind();

            RGP.BLL.UserGroup ugbll = new RGP.BLL.UserGroup();
            DataView dvList = new DataView(ugbll.GetUserGroupList("").Tables[0]);
            LoadGroupList("0", 0, dvList);
        }

        /// <summary>
        /// 加载用户分组
        /// </summary>
        /// <param name="UG_TID">分类上级ID</param>
        /// <param name="Depth">分类级别深度</param>
        protected void LoadGroupList(string UG_TID, int Depth,DataView dvList)
        {           
            dvList.RowFilter = "UG_SuperiorID=" + UG_TID;  //过滤父节点 
            foreach (DataRowView dv in dvList)
            {
                string depth = string.Empty;
                for (int i = 0; i < Depth; i++)
                {
                    depth = depth + "-";
                }
                GroupList.Items.Add(new ListItem(depth + dv["UG_Name"].ToString(), dv["UG_ID"].ToString()));

                LoadGroupList(dv["UG_ID"].ToString(), int.Parse(dv["UG_Depth"].ToString()) + 1, dvList);  //递归 
            }
        }

        protected void btn_createuser_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["VerifyCode"] == null)
            {
                JScript.Alert("您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统");
            }
            //验证码是否证确
            else if (String.Compare(Request.Cookies["VerifyCode"].Value, Request.Form["txt_verify"].Trim().ToUpper(), true) == 0)
            {
                //检测提交数据是否为空
                if (Request.Form["txt_name"] != "" &&
                    Request.Form["txt_password"] != "" &&
                    Request.Form["txt_repassword"] != "" &&
                    Request.Form["txt_email"] != "" &&
                    Request.Form["txt_verify"] != "")
                {
                    //两次密码必须输入必须相同
                    if (String.Compare(Request.Form["txt_password"].Trim(), Request.Form["txt_repassword"].Trim(), true) == 0)
                    {
                        //数据验证
                        if (VerifyData.IsEmail(Request.Form["txt_email"].Trim()) && VerifyData.IsName(Request.Form["txt_name"].Trim()))
                        {
                            RGP.BLL.Users bll = new RGP.BLL.Users();
                            RGP.Model.Users model = new RGP.Model.Users();

                            model.UserName = Request.Form["txt_name"].Trim();
                            model.Password = SecurityEncryption.MD5(Request.Form["txt_repassword"].Trim().ToLower(), 32);
                            model.Email = Request.Form["txt_email"].Trim();
                            int rid = 0;
                            if (RGP_Value.IsVerifyUser)
                            {
                                //读取系统设置
                                model.UserGroup = RGP_Value.initGroupID;
                                rid = RGP_Value.InitRoleID;
                            }
                            else
                            {
                                //让用户选择                                
                                model.UserGroup = int.Parse(GroupList.SelectedValue);
                                rid = int.Parse(RoleList.SelectedValue);
                            }

                            model.IsLimit = false;

                            if (!bll.UserExists(model.UserName))
                            {
                                int n=bll.CreateUser(model);
                                if ( n>= 1)
                                {
                                    if (rid != 0)
                                    {
                                        bll.addUserRole(n, rid);
                                    }
                                    JScript.AlertAndRedirect("注册成功!", "login.aspx");
                                }
                                else
                                {
                                    JScript.Alert("注册失败!");
                                }
                            }
                            else
                            {
                                JScript.Alert("用户已经存在，请更换后重试!");
                            }
                        }
                    }
                }
            }
            else
            {
                JScript.Alert("验证码错误!");
            }

            //重载状态
            if (!RGP_Value.IsVerifyUser)
            {
                isrole = "true";
            }
            else
            {
                isrole = "false";
            }
        }
    }
}
